/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.model;

import java.util.List;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import org.internna.iwebjtracker.activity.ActivityHolder;
import org.internna.iwebjtracker.activity.ActivityHolderImpl;
import org.internna.iwebjtracker.activity.model.RecordedActivity.ActivityType;
import org.internna.iwebjtracker.project.model.Module;
import org.internna.iwebjtracker.project.model.Project;
import org.internna.iwebjtracker.project.model.Version;

/**
 * Manages activities generated by other project operations.
 *
 * @author Jose Noheda
 * @since 1.0
 */
@MappedSuperclass
public abstract class ProjectActivityManager<M extends Module<P, M>, V extends Version<TrackerUser, P, V>, P extends Project<DefaultConnectionDetails, M, V, TrackerUser, P>> extends ProjectDataHolder<M, V, P> implements ActivityHolder<Activity, TrackerUser> {

    private static final long serialVersionUID = 1876611320987654321L;

    @Transient private ActivityHolderImpl holder;

    private ActivityHolderImpl getOrCreateHolder() {
        if (holder == null) {
            holder = new ActivityHolderImpl();
        }
        return holder;
    }

    /**
     * Delegates the logic to ActivityHolderImpl.
     *
     * @return a non null unmodifiable collection
     */
    @Override public final List<Activity> getPerformedActivities() {
        return getOrCreateHolder().getPerformedActivities();
    }

    /**
     * Delegates the logic to ActivityHolderImpl.
     */
    @Override public final void clear() {
        getOrCreateHolder().clear();
    }

    /**
     * Delegates the logic to ActivityHolderImpl.
     *
     * @param activity any
     */
    @Override public final void store(Activity activity) {
        getOrCreateHolder().store(activity);
    }

    /**
     * Creates a default activity filled with the current context values.
     *
     * @param type any (no operation performed on null)
     * @param user any (nulls may fail later)
     */
    @Override public final Activity createActivity(ActivityType type, TrackerUser user) {
        Activity activity = getOrCreateHolder().createActivity(type, user, isPublicView());
        onCreateActivity(activity);
        return activity;
    }

    /**
     * Extension point inherited by subclasses to perform operations on activity creation.
     *
     * @param activity a non null activity
     */
    protected abstract void onCreateActivity(Activity activity);

}
